package com.ja.openchat.ui.me;

import static com.zy.openchat.Constants.PAGE_COUNT;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.google.gson.JsonObject;
import com.ja.openchat.BR;
import com.ja.openchat.R;
import com.ja.openchat.databinding.FragmentTransactionRecordBinding;
import com.ja.openchat.viewmodels.TransactionRecordViewModel;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgEvent;
import com.zy.openchat.core.manager.TgResultBlock;
import com.zy.openchat.core.model.wallet.WalletOrderInfo;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;
import java.util.Map;

import me.goldze.mvvmhabit.base.BaseFragment;

//交易记录界面
public class TransactionRecordFragment extends BaseFragment<FragmentTransactionRecordBinding, TransactionRecordViewModel> {

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_transaction_record;
    }
    private TradeRecordAdapter mAdapter;
    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initView() {
        binding.refreshLayout.setEnableLoadMore(false);
    }
    protected int currentPage = 1;
    @Override
    public void initData() {
        mAdapter = new TradeRecordAdapter();
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        binding.recyclerView.setAdapter(mAdapter);
        binding.refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                currentPage = 1;
                requestData(currentPage);
            }
        });
        mAdapter.getLoadMoreModule().setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                currentPage++;
                requestData(currentPage);
            }
        });
        binding.refreshLayout.autoRefresh();
    }


    public void requestData(final int page) {
        TelegramManager.getTelegramManager().queryWalletOrderListCall(getContext(), PAGE_COUNT, currentPage, new TgResultBlock<List<WalletOrderInfo>>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, List<WalletOrderInfo> obj) {
                finishRefreshAndLoad(page,obj);

            }

            @Override
            public void onTimeout(Map<String, Object> request) {

            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTgEvent(final TgEvent tg_event) {
        if(TelegramManager.EUser_TgEvent.EUser_Td_UpdateContactInfo == tg_event.event){
            mAdapter.notifyDataSetChanged();
        }
    }

    protected void finishRefreshAndLoad(int page,List<WalletOrderInfo> objects){
        if(page == 1){
            binding.refreshLayout.finishRefresh();
            mAdapter.setNewInstance(objects);
        }else{
            if(objects!=null&&objects.size()>0){
                mAdapter.addData(objects);
            }
        }
        if(objects==null||objects.size()<PAGE_COUNT){
            mAdapter.getLoadMoreModule().loadMoreEnd(true);
        }else{
            mAdapter.getLoadMoreModule().loadMoreComplete();
        }
    }

}